    //
//  MyScrollViewController.m
//  Benz
//
//  Created by Tracy E on 10-9-17.
//  Copyright 2010 tracy.cpp@gmail.com. All rights reserved.
//

#import "DefaultViewController.h"
#import "SmartRoadShowAppDelegate.h"
#import "CalendarViewController.h"
#import "MyTabBarController.h"
#import "MyTabBar.h"
#import "MyNavigationController.h"
#import "RS1ViewController.h"
#import "RS2ViewController.h"
#import "RS3ViewController.h"
#import "ArtEvent.h"
#import "DetailWebViewController.h"
#import "DateUtil.h"
#import "ArtTour.h"


@interface DefaultViewController()
-(void)calendarAction;
-(void)displayInfoOnDefaultView;
@end


@implementation DefaultViewController
@synthesize myPageControl, myTabBarController;
@synthesize stationArray,eventArray;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"defaultBG.png"]]];
		
	scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 360)];
	scrollView.contentSize = CGSizeMake(320 * 4, 360);
	scrollView.pagingEnabled = YES;
	scrollView.delegate = self;
	scrollView.showsHorizontalScrollIndicator = NO;

	
	myPageControl = [[NSMutableArray alloc] init];
	pageBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 25 * 5, 30)];

	[self displayInfoOnDefaultView];
		
	pageBackground.center = CGPointMake(160, 370);
	[self.view addSubview:pageBackground];
	[pageBackground release];
	[self.view addSubview:scrollView];
	[scrollView release];

	UIButton *calenderButton = [UIButton buttonWithType:UIButtonTypeCustom];
	[calenderButton addTarget:self action:@selector(calendarAction) forControlEvents:UIControlEventTouchUpInside];
	[calenderButton setFrame:CGRectMake(245, 300, 60, 50)];
	[calenderButton setImage:[UIImage imageNamed:@"calenderButton.png"] forState:UIControlStateNormal];
	[self.view addSubview:calenderButton];
}


//在default页面上显示四屏信息.
-(void)displayInfoOnDefaultView{
	
	for (int i = 0; i < 4; i++) {
		
		UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
		[imageButton setTag:i];
		[imageButton setFrame:CGRectMake(5 + 320 * i, 5, 310, 350)];
//		[imageButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"0%@.png",[NSNumber numberWithInt:i]]]
//					 forState:UIControlStateNormal];
//		[imageButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"0%@.png",[NSNumber numberWithInt:i]]]
//					 forState:UIControlStateHighlighted];		
		[imageButton addTarget:self action:@selector(seeDetailInfo:) forControlEvents:UIControlEventTouchUpInside];
		
		[imageButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"0%@.png",[NSNumber numberWithInt:i]]] forState:UIControlStateNormal];
		[imageButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"0%@.png",[NSNumber numberWithInt:i]]] forState:UIControlStateHighlighted];

		
		[imageButton setContentMode:UIViewContentModeScaleAspectFit];
		
		
		label = [[UILabel alloc] initWithFrame:CGRectMake(20, 250, 250, 100)];
		label.numberOfLines = 0;
		label.backgroundColor = [UIColor clearColor];
		[imageButton addSubview:label];
		[label release];
		
		switch (i) {
                
			case 0:{
				SmartRoadShowAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
				self.stationArray = [appDelegate.rs3VC stationArray];
				
				NSMutableArray *showingCity = [[NSMutableArray alloc] init];
				
				//首先在路演信息里面找
				
				NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar] autorelease];
				
				NSDateComponents* comps = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
										   NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | 
										   NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
				
				
				NSString *year = [NSString stringWithFormat:@"%d",comps.year];
				NSString *month = nil;
				NSString *day = nil;
				
				if (comps.month < 10) {
					month = [NSString stringWithFormat:@"0%d",comps.month];
				}
				else {
					month = [NSString stringWithFormat:@"%d",comps.month];
				}
				
				if (comps.day < 10) {
					day = [NSString stringWithFormat:@"0%d",comps.day];
				}
				else {
					day = [NSString stringWithFormat:@"%d",comps.day];
				}
				
				NSString *currentDate = [NSString stringWithFormat:@"%@-%@-%@",year,month,day];
				
				
				for (int i = 0; i < [self.stationArray count]; i++) {
					
					NSString *start = [[self.stationArray objectAtIndex:i] _starttime];
					NSString *end = [[self.stationArray objectAtIndex:i] _endtime];
					if ([DateUtil diffBetweenDateStr:currentDate andDateStr:start] && [DateUtil diffBetweenDateStr:end andDateStr:currentDate]) {
						[showingCity addObject:[[self.stationArray objectAtIndex:i] _city]];
						currentTourIndex = i;
					}
				}
				if ([showingCity count] == 1) {
					label.text = [NSString stringWithFormat:@"smart“都市新主张”巡展活动\n%@站正在进行中！\n快来参加！",[showingCity objectAtIndex:0]];
				    firstScreenFrom = YES;
				}
				else if([showingCity count] == 2){
					label.text = [NSString stringWithFormat:@"smart“都市新主张”巡展活动\n%@站、%@站正在进行中！\n快来参加！",[showingCity objectAtIndex:0],[showingCity objectAtIndex:1]];
				    firstScreenFrom = YES;
				}
				currentTourIndex = currentTourIndex - [showingCity count] + 1;
				label.font = [UIFont boldSystemFontOfSize:15];
                
				//路演信息里面没有，就到艺术地图里面找
				if([showingCity count] == 0){
					self.eventArray = appDelegate.rs1VC.eventArray;
					
					for (int i = 0; i < [self.eventArray count]; i++) {
						NSString *start = [[self.eventArray objectAtIndex:i] _starttime];
						NSString *end = [[self.eventArray objectAtIndex:i] _endtime];
						
						if ([DateUtil diffBetweenDateStr:currentDate andDateStr:start] && [DateUtil diffBetweenDateStr:end andDateStr:currentDate]) {
							currentEventIndex = i;
							break;
						}
					}
					if ([self.eventArray count] > currentEventIndex) {
						label.text = [NSString stringWithFormat:@"%@\n正在进行中！快来参加！",[[self.eventArray objectAtIndex:currentEventIndex] _title]];
					}
					firstScreenFrom = NO;
				}
				[showingCity release];
				break;
			}
				default:
				break;
		}
		
		
		
		[scrollView addSubview:imageButton];
		
		
		UIButton *point = [UIButton buttonWithType:UIButtonTypeCustom];
		[point addTarget:self action:@selector(segmentedControlPressed:) forControlEvents:UIControlEventTouchUpInside];
		[point setFrame:CGRectMake(15 + 25*i, 7.5, 15, 15)];
		[point setTag:i];
		if (i == 0) {
			[point setImage:[UIImage imageNamed:@"red.png"]  forState:UIControlStateNormal];
		}
		else {
			[point setImage:[UIImage imageNamed:@"gray.png"] forState:UIControlStateNormal];
		}
		[pageBackground addSubview:point];
		[myPageControl addObject:point];
		
	}
	
}

#pragma mark -no use
////如果城市信息在另一个线程获取完毕,那么刷新label
-(void)refreshLabel:(NSMutableArray*)stationArr{
	self.stationArray = stationArr;
	[self displayInfoOnDefaultView];
	//label.text = [NSString stringWithFormat:@"路演%@站活动正在进行中\n快来参加！",cityName];
}

#pragma mark -button action

- (void)seeDetailInfo:(UIButton *)sender{
	
	UIButton* button = (UIButton*)sender;	

	if(button.tag == 0){  //点击第一副图
		if (firstScreenFrom) {
			[self.myTabBarController selectControllerAtIndex:0];
			RS3ViewController* rs3VC = (RS3ViewController*)[((MyNavigationController*)[self.myTabBarController.viewControllers objectAtIndex:0]) topViewController];
			[rs3VC.segmentControl setSelectedSegmentIndex:0];
			[rs3VC segmentedControl:rs3VC.segmentControl segmentDidChangeToIndex:0];
			[rs3VC ScrollViewScrollToCityIndex:currentTourIndex];
		} else {
			[self.myTabBarController selectControllerAtIndex:1];
			RS1ViewController* rs1VC = (RS1ViewController*)[((MyNavigationController*)[self.myTabBarController.viewControllers objectAtIndex:1]) topViewController];
			[rs1VC.segmentControl setSelectedSegmentIndex:0];
			[rs1VC segmentedControl:rs1VC.segmentControl segmentDidChangeToIndex:0];
		}


	}
	if(button.tag == 1){ //点击第2副图
		[self.myTabBarController selectControllerAtIndex:0];
		RS3ViewController* rs3VC = (RS3ViewController*)[((MyNavigationController*)[self.myTabBarController.viewControllers objectAtIndex:0]) topViewController];
		[rs3VC.segmentControl setSelectedSegmentIndex:0];
		[rs3VC segmentedControl:rs3VC.segmentControl segmentDidChangeToIndex:0];
		
	}

	if(button.tag == 2){ //点击第3副图
        [self.myTabBarController selectControllerAtIndex:0];
		RS3ViewController* rs3VC = (RS3ViewController*)[((MyNavigationController*)[self.myTabBarController.viewControllers objectAtIndex:0]) topViewController];
		[rs3VC.segmentControl setSelectedSegmentIndex:2];
		[rs3VC segmentedControl:rs3VC.segmentControl segmentDidChangeToIndex:2];


	}

	if(button.tag == 3){ //点击第4副图
		SmartRoadShowAppDelegate* delegate = [UIApplication sharedApplication].delegate;
		[delegate.tabBarController setContentView:delegate.planViewController.view];
	}
	
}


- (void)segmentedControlPressed:(UIButton *)sender{
	
	[scrollView scrollRectToVisible:CGRectMake(320 * sender.tag, 0, 320, 360) animated:NO];
}

- (void)scrollViewDidScroll:(UIScrollView *)myScrollView{
	
	CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
	if (page >= 0 && page < 4) {
		 [self currentPage:page];
	}
}

- (void)currentPage:(int)_currentPage{
	
	for (int i = 0; i < 4; i++) {
		[[myPageControl objectAtIndex:i] setImage:[UIImage imageNamed:@"gray.png"] forState:UIControlStateNormal];
	}
	
	UIButton *point = [myPageControl objectAtIndex:_currentPage];
	[point setImage:[UIImage imageNamed:@"red.png"] forState:UIControlStateNormal];
}

-(void)calendarAction{
	
	SmartRoadShowAppDelegate* delegate = [UIApplication sharedApplication].delegate;
	[delegate.tabBarController setContentView:delegate.navigationController_calendar.view];
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	scrollView = nil;
	pageBackground = nil;
	pageControl = nil;
}

- (void)dealloc {
	[scrollView release];
	[pageBackground release];
	[stationArray release];
	[eventArray release];
	[myPageControl release];
    [super dealloc];
}


@end
